var axios = require('axios');
var qs = require('qs');
const { url } = require('../../config/config');

var baseUrl = `http://${url}:31112`;
var config = {
    method: '',
    headers: {}
};
module.exports = {
    async requestGetWithAuth(request) {
        return new Promise((resolve, reject) => {
            config.method = "get"
            config.url = baseUrl + request.url
            config.headers.Authorization = 'Basic YWRtaW46YWRtaW4='
            config.data = typeof (request.data) == 'string' ? request.data : qs.stringify(request.data == null ? {} : request.data);
            var tmp_config = config;
            try {
                axios(config).then(response => {
                    console.log({
                        "地址": request.url,
                        "入参": config.data,
                        "回参": JSON.stringify(response.data),
                        "请求": JSON.stringify(tmp_config),
                    });
                    if (request.success) {
                        resolve(response.data);
                        request.success(response.data);
                    }
                });
            } catch (error) {
                console.error(error);
            }
        })
    },
    async requestPostWithAuth(request) {
        return new Promise((resolve, reject) => {
            config.method = "post"
            config.url = baseUrl + request.url
            config.headers.Authorization = 'Basic YWRtaW46YWRtaW4='
            config.data = typeof (request.data) == 'string' ? request.data : qs.stringify(request.data == null ? {} : request.data);
            var tmp_config = config;
            try {
                axios(config).then(response => {
                    console.log({
                        "地址": request.url,
                        "入参": config.data,
                        "回参": JSON.stringify(response.data),
                        "请求": JSON.stringify(tmp_config),
                    });
                    if (request.success) {
                        // resolve(response.data);
                        request.success(response.data);
                    }
                });
            } catch (error) {
                console.error(error);
            }
        })
    }
};